﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class EmployerGateway : DBGateway
    {
        public string CreateEmpProfile(Employer employer)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblEmpProfile values(@CompanyName,@Designation,@CompanyAddress,@BillingAddress,@Area,@WebAddress,@CompanyLogo,@EmployerUserId,@LocationId,@BusinessTypeId)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@CompanyName", SqlDbType.NVarChar).Value = employer.CompanyName;
                CommandObj.Parameters.Add("@Designation", SqlDbType.NVarChar).Value = employer.Designation;
                CommandObj.Parameters.Add("@CompanyAddress", SqlDbType.NVarChar).Value = employer.CompanyAddress;
                CommandObj.Parameters.Add("@BillingAddress", SqlDbType.NVarChar).Value = employer.BillingAddress;
                CommandObj.Parameters.Add("@Area", SqlDbType.NVarChar).Value = employer.Area;
                CommandObj.Parameters.Add("@WebAddress", SqlDbType.NVarChar).Value = employer.WebAddress;
                CommandObj.Parameters.Add("@CompanyLogo", SqlDbType.NVarChar).Value = employer.CompanyLogo;
                CommandObj.Parameters.Add("@EmployerUserId", SqlDbType.Int).Value = employer.EmployerUser.Id;
                CommandObj.Parameters.Add("@LocationId", SqlDbType.Int).Value = employer.Location.Id;//ask to selim vay about city manage
                CommandObj.Parameters.Add("@BusinessTypeId", SqlDbType.Int).Value = employer.BusinessType.Id;
                CommandObj.ExecuteNonQuery();
                return "New employee created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Employeer Not Save, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }


        public bool HasThisEmpProfile(Employer employer)
        {

            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblEmpProfile Where EmpId='{0}'", employer.Id);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Employeer Not Save, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }


        public List<Employer>GetAllEmployer()
        {
            try
            {
                List<Employer> employers=new List<Employer>();
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("select * from tblEmpProfile");
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Employer employer=new Employer();
                    EmployerUser empuser=new EmployerUser();
                    employer.EmployerUser = empuser;
                    employer.EmployerUser.Name = reader[1].ToString();
                    employer.EmployerUser.Email = reader[2].ToString();
                    employer.CompanyName = reader[3].ToString();
                    employer.CompanyAddress = reader[4].ToString();
                    employers.Add(employer);
                }
                return employers;
            }
            catch (Exception)
            {

                throw new Exception("Error occured while Reading Employer List, Please Try again !");
            }

        }
    }

}